
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>圆片冲压与成本计算器</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }
        
        .container {
            max-width: 900px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 30px;
            position: relative;
        }
        
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
            font-weight: 600;
        }
        
        .tabs {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #e0e0e0;
        }
        
        .tab {
            padding: 10px 20px;
            cursor: pointer;
            border: none;
            background: none;
            font-size: 16px;
            color: #555;
            position: relative;
            transition: color 0.3s;
        }
        
        .tab.active {
            color: #3498db;
            font-weight: bold;
        }
        
        .tab.active::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            right: 0;
            height: 2px;
            background: #3498db;
        }
        
        .tab-content {
            display: none;
        }
        
        .tab-content.active {
            display: block;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #34495e;
        }
        
        input, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        
        input:focus, select:focus {
            outline: none;
            border-color: #3498db;
        }
        
        .btn {
            background-color: #3498db;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            transition: background-color 0.3s;
        }
        
        .btn:hover {
            background-color: #2980b9;
        }
        
        .result-section {
            margin-top: 30px;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 8px;
            border-left: 5px solid #3498db;
        }
        
        .result-section h2 {
            color: #2c3e50;
            margin-bottom: 15px;
        }
        
        .result-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
        }
        
        .result-item {
            background: white;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }
        
        .result-item strong {
            color: #34495e;
        }
        
        .chart-container {
            margin-top: 30px;
            height: 300px;
            position: relative;
        }
        
        .footer {
            text-align: center;
            margin-top: 30px;
            color: #7f8c8d;
            font-size: 14px;
        }
        
        @media (max-width: 600px) {
            .container {
                padding: 20px;
            }
            
            .result-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>圆片冲压与成本计算器</h1>
        
        <div class="tabs">
            <button class="tab active" onclick="showTab('calculator')">计算器</button>
            <button class="tab" onclick="showTab('about')">使用说明</button>
        </div>
        
        <div id="calculator" class="tab-content active">
            <form id="calcForm">
                <div class="form-group">
                    <label for="diameter">圆片直径 (mm):</label>
                    <input type="number" id="diameter" name="diameter" min="1" step="0.1" required>
                </div>
                
                <div class="form-group">
                    <label for="material">材料类型:</label>
                    <select id="material" name="material" required>
                        <option value="">请选择材料</option>
                        <option value="steel">不锈钢</option>
                        <option value="aluminum">铝</option>
                        <option value="copper">铜</option>
                        <option value="brass">黄铜</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="thickness">材料厚度 (mm):</label>
                    <input type="number" id="thickness" name="thickness" min="0.1" step="0.01" required>
                </div>
                
                <div class="form-group">
                    <label for="price">材料单价 (元/kg):</label>
                    <input type="number" id="price" name="price" min="0.01" step="0.01" required>
                </div>
                
                <div class="form-group">
                    <label for="lossRate">冲压损耗率 (%):</label>
                    <input type="number" id="lossRate" name="lossRate" min="0" max="50" step="0.1" required>
                </div>
                
                <button type="submit" class="btn">计算成本</button>
            </form>
            
            <div id="results" class="result-section" style="display: none;">
                <h2>计算结果</h2>
                <div class="result-grid">
                    <div class="result-item">
                        <strong>单件用料面积:</strong>
                        <span id="area">-</span> mm²
                    </div>
                    <div class="result-item">
                        <strong>理论重量:</strong>
                        <span id="weight">-</span> g
                    </div>
                    <div class="result-item">
                        <strong>材料成本:</strong>
                        <span id="materialCost">-</span> 元
                    </div>
                    <div class="result-item">
                        <strong>冲压损耗成本:</strong>
                        <span id="lossCost">-</span> 元
                    </div>
                    <div class="result-item">
                        <strong>总成本:</strong>
                        <span id="totalCost">-</span> 元
                    </div>
                </div>
                
                <div class="chart-container">
                    <canvas id="costChart"></canvas>
                </div>
            </div>
        </div>
        
        <div id="about" class="tab-content">
            <h2>使用说明</h2>
            <p>本计算器用于估算圆片冲压加工的材料成本。请输入以下参数：</p>
            <ul>
                <li><strong>圆片直径：</strong>冲压件的直径，单位为毫米</li>
                <li><strong>材料类型：</strong>选择加工材料，系统将自动应用对应密度</li>
                <li><strong>材料厚度：</strong>板材厚度，单位为毫米</li>
                <li><strong>材料单价：</strong>每公斤材料的价格</li>
                <li><strong>冲压损耗率：</strong>加工过程中的材料损耗百分比</li>
            </ul>
            <p>计算结果包括用料面积、重量、材料成本、损耗成本和总成本，并以图表形式展示成本构成。</p>
            
            <h3>材料密度参考</h3>
            <ul>
                <li>不锈钢：7.93 g/cm³</li>
                <li>铝：2.70 g/cm³</li>
                <li>铜：8.96 g/cm³</li>
                <li>黄铜：8.40 g/cm³</li>
            </ul>
        </div>
        
        <div class="footer">
            <p>© 2025 圆片冲压与成本计算器 | 仅供参考，实际成本可能因工艺、批量等因素有所不同</p>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/Chart.js/4.4.0/chart.min.js"></script>
    <script>
        // 材料密度表 (g/cm³)
        const materialDensities = {
            steel: 7.93,
            aluminum: 2.70,
            copper: 8.96,
            brass: 8.40
        };

        let costChart = null;

        function showTab(tabName) {
            const tabs = document.querySelectorAll('.tab');
            const contents = document.querySelectorAll('.tab-content');
            
            tabs.forEach(tab => tab.classList.remove('active'));
            contents.forEach(content => content.classList.remove('active'));
            
            event.target.classList.add('active');
            document.getElementById(tabName).classList.add('active');
        }

        document.getElementById('calcForm').addEventListener('submit', function(e) {
            e.preventDefault();
            calculateCost();
        });

        function calculateCost() {
            const diameter = parseFloat(document.getElementById('diameter').value);
            const material = document.getElementById('material').value;
            const thickness = parseFloat(document.getElementById('thickness').value);
            const price = parseFloat(document.getElementById('price').value);
            const lossRate = parseFloat(document.getElementById('lossRate').value);

            if (!diameter || !material || !thickness || !price || lossRate === '') {
                alert('请填写所有必填项');
                return;
            }

            // 计算面积 (mm²)
            const radius = diameter / 2;
            const area = Math.PI * radius * radius;

            // 计算体积 (mm³)
            const volume = area * thickness;

            // 转换为 cm³ 并计算重量 (g)
            const volumeCm3 = volume / 1000;
            const density = materialDensities[material];
            const weight = volumeCm3 * density;

            // 计算材料成本
            const weightKg = weight / 1000;
            const materialCost = weightKg * price;

            // 计算损耗成本
            const lossCost = materialCost * (lossRate / 100);

            // 总成本
            const totalCost = materialCost + lossCost;

            // 显示结果
            document.getElementById('area').textContent = area.toFixed(2);
            document.getElementById('weight').textContent = weight.toFixed(2);
            document.getElementById('materialCost').textContent = materialCost.toFixed(3);
            document.getElementById('lossCost').textContent = lossCost.toFixed(3);
            document.getElementById('totalCost').textContent = totalCost.toFixed(3);

            document.getElementById('results').style.display = 'block';

            // 绘制图表
            drawChart(materialCost, lossCost);
        }

        function drawChart(materialCost, lossCost) {
            const ctx = document.getElementById('costChart').getContext('2d');
            
            if (costChart) {
                costChart.destroy();
            }

            costChart = new Chart(ctx, {
                type: 'pie',
                data: {
                    labels: ['材料成本', '损耗成本'],
                    datasets: [{
                        data: [materialCost, lossCost],
                        backgroundColor: ['#3498db', '#e74c3c'],
                        borderWidth: 2,
                        borderColor: '#fff'
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            position: 'bottom',
                            labels: {
                                padding: 20,
                                font: {
                                    size: 14
                                }
                            }
                        },
                        tooltip: {
                            callbacks: {
                                label: function(context) {
                                    const label = context.label || '';
                                    const value = context.parsed;
                                    const total = context.dataset.data.reduce((a, b) => a + b, 0);
                                    const percentage = ((value / total) * 100).toFixed(1);
                                    return `${label}: ¥${value.toFixed(3)} (${percentage}%)`;
                                }
                            }
                        }
                    }
                }
            });
        }
    </script>
</body>
</html>
